package com.boot.system.controller;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.http.HttpStatus;
import com.boot.framework.aop.Log;
import com.boot.framework.common.R;
import com.boot.framework.oss.CloudStorageConfig;
import com.boot.framework.oss.OSSFactory;
import com.boot.system.dto.SysFileDTO;
import com.boot.system.entity.SysFileEntity;
import com.boot.system.wrapper.SysFileWrapper;
import com.boot.system.service.ISysFileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yangfan
 * @date 2024/2/1
 * @Description
 */
@RestController
@AllArgsConstructor
@RequestMapping("/system/file")
@Tag(name = "文件管理")
public class SysFileController {

    private final ISysFileService sysFileService;

    private final CloudStorageConfig config;

    @GetMapping("page")
    @Operation(summary = "分页")
    public R page(@RequestParam Map<String, Object> params) {

        return R.ok(sysFileService.selectPage(params));
    }

    @GetMapping("list")
    @Operation(summary = "集合")
    public R list(Map<String, Object> params) {
        return R.ok(sysFileService.selectList(params));
    }


    @PostMapping
    public R insert(@RequestBody SysFileDTO sysFileDTO) {
        return R.ok(sysFileService.save(SysFileWrapper.build().dtoEntity(sysFileDTO)));
    }


    @PutMapping
    public R update(@RequestBody SysFileDTO sysFileDTO) {
        return R.ok(sysFileService.updateById(SysFileWrapper.build().dtoEntity(sysFileDTO)));
    }


    @GetMapping
    @Operation(summary = "查看")
    public R view(@RequestParam String id) {
        return R.ok(sysFileService.getById(id));
    }

    @DeleteMapping
    @Log(title = "文件", content = "删除")
    @Operation(summary = "删除")
    public R delete(String ids) {
        return R.ok(sysFileService.removeBatchByIds(Arrays.asList(StringUtils.split(ids, ","))));
    }

    @PostMapping("/upload")
    @Log(title = "文件管理", content = "上传")
    @Operation(summary = "上传")
    public R upload(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return new R().setCode(HttpStatus.HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE);
        }
        //上传文件
        String suffix = FileNameUtil.getSuffix(file.getOriginalFilename());
        String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);

        //保存文件信息
        SysFileEntity ossEntity = new SysFileEntity();
        ossEntity.setPath(url);
        ossEntity.setBucketName(config.getBucketName());
        ossEntity.setOriginal(file.getOriginalFilename());
        ossEntity.setFileName(url.substring(url.lastIndexOf("/") + 1));
        ossEntity.setFileSize(file.getSize());
        ossEntity.setType(url.substring(url.lastIndexOf(".") + 1));
        sysFileService.save(ossEntity);
        Map<String, Object> ok = new HashMap<>(1);
        ok.put("src", url);

        return R.ok(ok);
    }
}
